package com.woniu.consume;

import com.alibaba.fastjson2.JSON;
import com.rabbitmq.client.Channel;
import com.woniu.entity.SecKillOrder;
import com.woniu.mapper.SecKillMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class AddKillOrderConsume {
    @Autowired
    private SecKillMapper secKillMapper;

    @RabbitListener(queues = "addKillOrderQueue")
    public void receive(String msg, Message message, Channel channel){
        try {
            SecKillOrder secKillOrder = JSON.parseObject(msg, SecKillOrder.class);
            secKillMapper.addSecKillOrder(secKillOrder);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
